import time
import json
import requests
import re
import logging
logger = logging.getLogger(__name__)

def get_data_from_foundnet():
    timestamp = int(time.time() * 1000)
    url = "https://api.fund.eastmoney.com/f10/lsjz?callback=jQuery18304381043487436136_1721011050692&fundCode=013128&pageIndex=1&pageSize=10&startDate=&endDate=&_=%s" % (
        timestamp)
    # 定义headers
    headers = {
        'Host': 'api.fund.eastmoney.com',  # 例如，添加Authorization header
        'Content-Type': 'application/json',  # 例如，添加Content-Type header
        'Referer': 'https://fundf10.eastmoney.com/' , # 你可以添加自定义的header
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
    }
    # 发送GET请求
    response = requests.get(url, headers=headers)
    if hasattr(response,'status_code') and 200== response.status_code:
        if not hasattr(response, 'text') :
            logger.info('%s 获取信息失败' % url)
            res={}
        else:
            logger.info('%s 获取信息成功' % url)
            # 响应字符串
            jsonp_response = response.text
            # 使用正则表达式提取JSON字符串
            json_str = re.search(r'\((.*?)\)', jsonp_response).group(1)
            # 将JSON字符串解析为Python字典
            data = json.loads(json_str)
            res=data
    else:
        logger.info('%s 获取信息失败'%url)
        res={}
    return res

#https://fundf10.eastmoney.com/jjjz_013128.html
def get_eastmoney():
    # reslist="""{"Data":{"LSJZList":[{"FSRQ":"2024-08-28","DWJZ":"0.5821","LJJZ":"0.5821","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"-1.59","SGZT":"开放申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""},{"FSRQ":"2024-08-27","DWJZ":"0.5915","LJJZ":"0.5915","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"0.05","SGZT":"开放申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""},{"FSRQ":"2024-08-26","DWJZ":"0.5912","LJJZ":"0.5912","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"0.66","SGZT":"开放申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""},{"FSRQ":"2024-08-23","DWJZ":"0.5873","LJJZ":"0.5873","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"-0.94","SGZT":"开放申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""},{"FSRQ":"2024-08-22","DWJZ":"0.5929","LJJZ":"0.5929","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"1.87","SGZT":"开放申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""},{"FSRQ":"2024-08-21","DWJZ":"0.5820","LJJZ":"0.5820","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"-1.77","SGZT":"开放申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""},{"FSRQ":"2024-08-20","DWJZ":"0.5925","LJJZ":"0.5925","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"-0.54","SGZT":"开放申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""},{"FSRQ":"2024-08-19","DWJZ":"0.5957","LJJZ":"0.5957","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"1.55","SGZT":"开放申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""},{"FSRQ":"2024-08-16","DWJZ":"0.5866","LJJZ":"0.5866","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"2.09","SGZT":"开放申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""},{"FSRQ":"2024-08-15","DWJZ":"0.5746","LJJZ":"0.5746","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"-0.38","SGZT":"开放申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""},{"FSRQ":"2024-08-14","DWJZ":"0.5768","LJJZ":"0.5768","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"-0.98","SGZT":"开放申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""},{"FSRQ":"2024-08-13","DWJZ":"0.5825","LJJZ":"0.5825","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"0.12","SGZT":"开放申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""},{"FSRQ":"2024-08-12","DWJZ":"0.5818","LJJZ":"0.5818","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"-0.24","SGZT":"开放申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""},{"FSRQ":"2024-08-09","DWJZ":"0.5832","LJJZ":"0.5832","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"1.87","SGZT":"限制大额申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""},{"FSRQ":"2024-08-08","DWJZ":"0.5725","LJJZ":"0.5725","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"-0.28","SGZT":"限制大额申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""},{"FSRQ":"2024-08-07","DWJZ":"0.5741","LJJZ":"0.5741","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"1.15","SGZT":"限制大额申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""},{"FSRQ":"2024-08-06","DWJZ":"0.5676","LJJZ":"0.5676","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"-0.12","SGZT":"限制大额申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""},{"FSRQ":"2024-08-05","DWJZ":"0.5683","LJJZ":"0.5683","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"-0.89","SGZT":"限制大额申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""},{"FSRQ":"2024-08-02","DWJZ":"0.5734","LJJZ":"0.5734","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"-2.40","SGZT":"限制大额申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""},{"FSRQ":"2024-08-01","DWJZ":"0.5875","LJJZ":"0.5875","SDATE":null,"ACTUALSYI":"","NAVTYPE":"1","JZZZL":"-1.14","SGZT":"限制大额申购","SHZT":"开放赎回","FHFCZ":"","FHFCBZ":"","DTYPE":null,"FHSP":""}],"FundType":"007","SYType":null,"isNewType":false,"Feature":"055,080,311,318,319"},"ErrCode":0,"ErrMsg":null,"TotalCount":690,"Expansion":null,"PageSize":20,"PageIndex":1}"""
    # res=json.loads(reslist)
    res =get_data_from_foundnet()
    res_list =res.get('Data',{}).get('LSJZList',[])
    res_list =sorted(res_list, key=lambda x: x['FSRQ'])
    res=[]
    for each in res_list:
        res.append({'found_code':'013128','found_name':f'汇添富恒生科技ETF',
                    'base_date':each['FSRQ'],
          'base_si_price':each['DWJZ'],'rate':each['JZZZL']})
    return res
